import http from "@/utils/http.ts";
import {Dict, R} from "@/api/common.ts";

enum Schedule {
    list = "jobs/list",
    jobs = "jobs",
    dict = "jobs/dict",
    cron = "jobs/cron"
}

export interface TaskList {
    bean: string;
    method: string[];
}

export interface JobList {
    id: number;
    bean: string;
    cron: string;
    method: string;
    status: number;
    remark: string;
    createTime: string;
    updateTime: string;
}

export interface JobPost {
    bean: string;

    cron: string;

    method: string;

    remark: string;
}

export interface JobPut {
    id: number;
    cron: string;
    remark: string;
}

/**
 * 获取可以执行的任务列表
 */
export function getTaskList() {
    return http<R<TaskList[]>>({
        url: Schedule.list,
        method: "get"
    })
}

/**
 * 获取任务列表
 */
export function getJobList() {
    return http<R<JobList[]>>({
        url: Schedule.jobs,
        method: "get"
    })
}

export function getDict() {
    return http<R<Dict[]>>({
        url: Schedule.dict,
        method: "get"
    })
}

/**
 * 开启任务
 */
export function start(id: number) {
    return http<R<null>>({
        url: `jobs/start/${id}`,
        method: "get"
    })
}

/**
 * 关闭任务
 */
export function stop(id: number) {
    return http<R<null>>({
        url: `jobs/stop/${id}`,
        method: "get"
    })
}

/**
 * 修改任务
 */
export function update(data: JobPut) {
    return http<R<null>>({
        url: "jobs",
        method: "put",
        data
    })
}

/**
 * 删除任务
 */
export function deleteTask(id: number) {
    return http<R<null>>({
        url: `jobs/${id}`,
        method: "delete"
    })
}

/**
 * 验证cron是否正确
 */
export function validateCron(cron: string) {
    return http<R<null>>({
        url: Schedule.cron,
        method: "get",
        params: {cron: cron}
    })
}

export function add(data: JobPost) {
    return http<R<null>>({
        url: "jobs",
        method: "post",
        data
    })
}